package fr.umlv.m2.recipes.ideas.dao;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import fr.umlv.m2.recipes.ideas.bean.KeywordBean;
import fr.umlv.m2.recipes.ideas.dao.interfaces.RecipesConcreteDAOInterface;
import fr.umlv.m2.recipes.ideas.table.enumerations.TableName;

/**
 * DAO uses to handle a keyword
 * @author Christophe Chillan et Younes Chandarli
 *
 */
public class KeywordDAO implements RecipesConcreteDAOInterface<KeywordBean> {
	
	private static final String TABLE_KEYWORD = TableName.KEYWORD.toString();
	
	private static final String COL_ID = "ID";	
	private static final String COL_KEYWORD = "KEYWORD";

	private static final int NUM_COL_ID = 0;
	
	private final StringBuilder CREATE_DB;
	
	/**
	 * Create a keyword DAO
	 */
	public KeywordDAO(){
		CREATE_DB = new StringBuilder();
		CREATE_DB.append("CREATE TABLE ");
		CREATE_DB.append(TABLE_KEYWORD).append(" ");
		CREATE_DB.append("(");
		CREATE_DB.append(COL_ID).append(" INTEGER PRIMARY KEY AUTOINCREMENT, ");
		CREATE_DB.append(COL_KEYWORD).append(" TEXT NOT NULL");
		CREATE_DB.append(");");
	}

	/**
	 * @see SQLiteOpenHelper
	 */
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(CREATE_DB.toString());
		
	}

	/**
	 * @see SQLiteOpenHelper
	 */
	public void onUpgrade(SQLiteDatabase db) {
		db.execSQL("DROP TABLE "+ TABLE_KEYWORD);
		onCreate(db);
		
	}

	/**
	 * Insert an keyword in the database
	 *  @param db
	 *  @param keyword
	 *  @return long - 1 if the entry has been inserted, 0 otherwise 
	 */
	public long insert(SQLiteDatabase db, KeywordBean keyword) {
		//Création d'un ContentValues (fonctionne comme une HashMap)
		ContentValues values = new ContentValues();
		//Création d'un ContentValues (fonctionne comme une HashMap)
		values.put(COL_KEYWORD, keyword.getKeyword());
		return db.insert(TABLE_KEYWORD, null, values);
	}

	/**
	 * update an keyword from the keyword id in parameters
	 * @param db
	 * @param keyword
	 * @param id
	 * @return integer - the number of entries uploaded
	 */
	public int update(SQLiteDatabase db, KeywordBean keyword, int id) {
		//Création d'un ContentValues (fonctionne comme une HashMap)
		ContentValues values = new ContentValues();
		//Création d'un ContentValues (fonctionne comme une HashMap)
		values.put(COL_KEYWORD, keyword.getKeyword());
		return db.update(TABLE_KEYWORD,values, COL_ID+ " = " +id, null);
	}
	
	

	/**
	 * remove an keyword from the id
	 * @return integer - the number of entries removed
	 */
	public int removeWithId(SQLiteDatabase db, int id) {
		return db.delete(TABLE_KEYWORD, COL_ID+ " = "+id, null);
	}
	
	/**
	 * Get the last id of the keywords
	 * @param db
	 * @return integer - the last id inserted in the database
	 */
	public int getLastInsertIdOfKeyword(SQLiteDatabase db) {
		StringBuilder query = new StringBuilder();
		query.append("SELECT max(").append(COL_ID).append(") ");
		query.append("from ").append(TABLE_KEYWORD);
		Cursor c = db.rawQuery(query.toString(), null);
		return cursorToMaxId(c);
	}
	
	private int cursorToMaxId(Cursor c){
		if(c.getCount()==0){
			return 0;
		}
		c.moveToFirst();
		return c.getInt(NUM_COL_ID);
	}

}
